package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.UserEcologyHatchUpperLimitDao;
import com.sc.nft.dto.EcologyHatchConfDao;
import com.sc.nft.dto.UserEcologyHatchRecordsDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.EcologyHatchCountDTO;
import com.sc.nft.entity.vo.PageHatchRecordsVO;
import com.sc.nft.enums.UserEcologyCastUpperLimitDetailsTypeEnum;
import com.sc.nft.enums.UserEcologyHatchUpperLimitDetailsTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologySmeltingService;
import com.sc.nft.service.UserEcologyHatchUpperLimitService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-08-08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserEcologyHatchUpperLimitServiceImpl implements UserEcologyHatchUpperLimitService {



    private final UserEcologyHatchUpperLimitDao userEcologyHatchUpperLimitDao;

    private final EcologyHatchConfDao ecologyHatchConfDao;

    private final UserEcologyHatchRecordsDao userEcologyHatchRecordsDao;


    @Override
    public UserEcologyHatchUpperLimit getOneByUserIdAndCollectionId(Long userId, Long collectionId) {
        return userEcologyHatchUpperLimitDao.getOneByUserIdAndCollectionId(userId, collectionId);
    }

    /**
     * 新增熔炼上限 并且添加熔炼上限日志
     */

    public void addCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count,
                                                UserEcologyHatchUpperLimitDetailsTypeEnum type, String eventsName, Long userRecordsId,Long ecologyHatchId,Integer sumRemainingCount) {


        UserEcologyHatchUpperLimit userEcologyHatchUpperLimit = userEcologyHatchUpperLimitDao.getOneByUserIdAndCollectionId(userId, collectionId);

        Integer beforeCount = 0;
        if(ObjectUtil.isNull(userEcologyHatchUpperLimit)){
            UserEcologyHatchUpperLimit insertUserEcologyHatchUpperLimit = new UserEcologyHatchUpperLimit();
            insertUserEcologyHatchUpperLimit.setUserId(userId);
            insertUserEcologyHatchUpperLimit.setCollectionId(collectionId);
            insertUserEcologyHatchUpperLimit.setUsableCount(count);
            insertUserEcologyHatchUpperLimit.setEcologyHatchId(ecologyHatchId);
            insertUserEcologyHatchUpperLimit.insert();
        }else{
            beforeCount = userEcologyHatchUpperLimit.getUsableCount();
            Assert.isTrue(userEcologyHatchUpperLimitDao.addCountByUserIdAndCollectionId(userId, collectionId, count),
                    () -> new GlobalRunTimeException("用户熔炼上限更新失败"));
        }

        UserEcologyHatchUpperLimitDetails userEcologyHatchUpperLimitDetails = new UserEcologyHatchUpperLimitDetails();
        userEcologyHatchUpperLimitDetails.setUserId(userId);
        userEcologyHatchUpperLimitDetails.setCollectionId(collectionId);
        userEcologyHatchUpperLimitDetails.setUserRecordsId(userRecordsId);
        userEcologyHatchUpperLimitDetails.setType(type);
        if (UserEcologyHatchUpperLimitDetailsTypeEnum.EXCHANGE.equals(type)){
            userEcologyHatchUpperLimitDetails.setTypeName(type.getDescription());
        }else {
            userEcologyHatchUpperLimitDetails.setTypeName(StrUtil.format("{}:{}",type.getDescription(),eventsName));
        }
        userEcologyHatchUpperLimitDetails.setUseCount(count);
        userEcologyHatchUpperLimitDetails.setBeforeCount(sumRemainingCount);
        userEcologyHatchUpperLimitDetails.setAfterCount(sumRemainingCount + count);
        userEcologyHatchUpperLimitDetails.setIsAdd(type.getIsAdd());
        userEcologyHatchUpperLimitDetails.insert();

    }

    /**
     * 减少熔炼上限 并且添加熔炼上限日志
     * @param userId
     * @param collectionId
     * @param count
     * @param type
     * @param eventsName
     * @param userRecordsId
     */
    @Override
    public void subUpperLimitCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, UserEcologyHatchUpperLimitDetailsTypeEnum type, String eventsName, Long userRecordsId,Integer beforeCount,Integer hatchDetails) {
        UserEcologyHatchUpperLimit userEcologyHatchUpperLimit = userEcologyHatchUpperLimitDao.getOneByUserIdAndCollectionId(userId, collectionId);
        Assert.notNull(userEcologyHatchUpperLimit, "用户熔炼上限不存在");

        Assert.isTrue(
                userEcologyHatchUpperLimitDao.subCountByUserIdAndCollectionId(userId, collectionId, count),
                () -> new GlobalRunTimeException("用户熔炼上限更新失败"));

        UserEcologyHatchUpperLimitDetails userEcologyHatchUpperLimitDetails = new UserEcologyHatchUpperLimitDetails();
        userEcologyHatchUpperLimitDetails.setUserId(userId);
        userEcologyHatchUpperLimitDetails.setCollectionId(collectionId);
        userEcologyHatchUpperLimitDetails.setUserRecordsId(userRecordsId);
        userEcologyHatchUpperLimitDetails.setType(type);
        userEcologyHatchUpperLimitDetails.setTypeName(type.getDescription());
        userEcologyHatchUpperLimitDetails.setUseCount(hatchDetails);
        userEcologyHatchUpperLimitDetails.setBeforeCount(beforeCount);
        userEcologyHatchUpperLimitDetails.setAfterCount(beforeCount - hatchDetails);
        userEcologyHatchUpperLimitDetails.setIsAdd(type.getIsAdd());
        userEcologyHatchUpperLimitDetails.insert();

    }

    /**
     * 添加熔炼上限日志
     * @param userId
     * @param collectionId
     * @param count
     * @param type
     * @param eventsName
     * @param userRecordsId
     */
    @Override
    public void addUserEcologyHatchUpperLimitDetails(Long userId, Long collectionId, Integer count, UserEcologyHatchUpperLimitDetailsTypeEnum type, String eventsName, Long userRecordsId,Integer beforeCount) {

        UserEcologyHatchUpperLimitDetails userEcologyHatchUpperLimitDetails = new UserEcologyHatchUpperLimitDetails();
        userEcologyHatchUpperLimitDetails.setUserId(userId);
        userEcologyHatchUpperLimitDetails.setCollectionId(collectionId);
        userEcologyHatchUpperLimitDetails.setUserRecordsId(userRecordsId);
        userEcologyHatchUpperLimitDetails.setType(type);
        userEcologyHatchUpperLimitDetails.setTypeName(type.getDescription());
        userEcologyHatchUpperLimitDetails.setUseCount(count);
        userEcologyHatchUpperLimitDetails.setBeforeCount(beforeCount);
        userEcologyHatchUpperLimitDetails.setAfterCount(beforeCount - count);
        userEcologyHatchUpperLimitDetails.setIsAdd(type.getIsAdd());
        userEcologyHatchUpperLimitDetails.insert();

    }



    /**
     * 后台增加系统初始上限 记录熔炼上限日志
     */
    public void addCountBySys(Long ecologyHatchId,Integer beforeCount,Integer afterCount,Integer count) {

        EcologyHatchConf ecologyHatchConf = ecologyHatchConfDao.getById(ecologyHatchId);
        Assert.notNull(ecologyHatchConf, () -> new GlobalRunTimeException("熔炼配置不存在"));
        List<UserEcologyHatchRecords> userEcologyHatchRecordsList = userEcologyHatchRecordsDao.getAllByEcologyHatchIdList(ecologyHatchId);
        for (UserEcologyHatchRecords userEcologyHatchRecords : userEcologyHatchRecordsList) {
            UserEcologyHatchUpperLimitDetails userEcologyHatchUpperLimitDetails = new UserEcologyHatchUpperLimitDetails();
            userEcologyHatchUpperLimitDetails.setUserId(userEcologyHatchRecords.getUserId());
            userEcologyHatchUpperLimitDetails.setCollectionId(userEcologyHatchRecords.getCollectionId());
            userEcologyHatchUpperLimitDetails.setUserRecordsId(0L);
            userEcologyHatchUpperLimitDetails.setType(UserEcologyHatchUpperLimitDetailsTypeEnum.SYS);
            userEcologyHatchUpperLimitDetails.setTypeName(UserEcologyHatchUpperLimitDetailsTypeEnum.SYS.getDescription());
            userEcologyHatchUpperLimitDetails.setUseCount(count);
            userEcologyHatchUpperLimitDetails.setBeforeCount(beforeCount);
            userEcologyHatchUpperLimitDetails.setAfterCount(afterCount);
            userEcologyHatchUpperLimitDetails.setIsAdd(UserEcologyHatchUpperLimitDetailsTypeEnum.SYS.getIsAdd());
            userEcologyHatchUpperLimitDetails.insert();
        }
    }

    /**
     * 后台减少系统初始上限 记录熔炼上限日志
     */
    public void subCountBySys(Long ecologyHatchId,Integer beforeCount,Integer afterCount,Integer count) {

        EcologyHatchConf ecologyHatchConf = ecologyHatchConfDao.getById(ecologyHatchId);
        Assert.notNull(ecologyHatchConf, () -> new GlobalRunTimeException("熔炼配置不存在"));
        List<UserEcologyHatchRecords> userEcologyHatchRecordsList = userEcologyHatchRecordsDao.getAllByEcologyHatchIdList(ecologyHatchId);
        for (UserEcologyHatchRecords userEcologyHatchRecords : userEcologyHatchRecordsList) {
            UserEcologyHatchUpperLimitDetails userEcologyHatchUpperLimitDetails = new UserEcologyHatchUpperLimitDetails();
            userEcologyHatchUpperLimitDetails.setUserId(userEcologyHatchRecords.getUserId());
            userEcologyHatchUpperLimitDetails.setCollectionId(userEcologyHatchRecords.getCollectionId());
            userEcologyHatchUpperLimitDetails.setUserRecordsId(0L);
            userEcologyHatchUpperLimitDetails.setType(UserEcologyHatchUpperLimitDetailsTypeEnum.SYS_SUB);
            userEcologyHatchUpperLimitDetails.setTypeName(UserEcologyHatchUpperLimitDetailsTypeEnum.SYS_SUB.getDescription());
            userEcologyHatchUpperLimitDetails.setUseCount(count);
            userEcologyHatchUpperLimitDetails.setBeforeCount(beforeCount);
            userEcologyHatchUpperLimitDetails.setAfterCount(afterCount);
            userEcologyHatchUpperLimitDetails.setIsAdd(UserEcologyHatchUpperLimitDetailsTypeEnum.SYS_SUB.getIsAdd());
            userEcologyHatchUpperLimitDetails.insert();
        }
    }





}
